package com.dingding.common.core.base.daox.base;

import java.util.Date;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateTemplate;

/**
 * 
 * @ClassName: AbstractBaseDao 
 * @author CHEHAUNBO 
 * @date 2016年8月21日 下午2:02:56
 * @since V2.4.0
 */
public abstract class AbstractBaseDao {

	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template = null;
	
	/**
	 * 
	 * @Title: getQuery 
	 * @Description: 获取Query 
	 * @param hql 
	 * @return Query 
	 * 
	 */
	protected Query getQuery(String hql) {
		return this.getSession().createQuery(hql);
	}

	/**
	 * 
	 * @Title: getQuery 
	 * @Description: 获取SQLQuery 
	 * @param sql 
	 * @return SQLQuery
	 * 
	 */
	protected Query getSQLQuery(String sql) {
		return this.getSession().createSQLQuery(sql);
	}

	/**
	 * 
	 * @Title: getSession 
	 * @Description: 获取session 
	 * @return Session 
	 * 
	 */
	public Session getSession() {
		return template.getSessionFactory().getCurrentSession();
	}
	
	/**
	 * 
	 * @Title: getSession 
	 * @Description: 获取session 
	 * @return Session 
	 * 
	 */
	public Session openSession() {
		return template.getSessionFactory().openSession();
	}

	/**
	 * 
	 * @Title: setParameters 
	 * @Description: 设置查询参数 
	 * @author CHEHUANBO
	 * @since V2.4.0
	 * 
	 */
	public void setParameters(Query query, Object[] parmas) {

		if (query.getQueryString().indexOf("?") != -1) {

			for (int i = 0; i < parmas.length; i++) {
				if (parmas[i] instanceof Date) {
					query.setParameter(i, (Date) parmas[i]);
				} else {
					query.setParameter(i, parmas[i]);
				}
			}
		}

	}

	public HibernateTemplate getTemplate() {
		return template;
	}

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

}
